var api          = require('API').rank,
    get_all_role = require('API').rbac.roles;

module.exports = ['$scope','$http', function ($scope,$http) {
    var _API = require('_API')($http);

    //岗位类型
    $scope.type = [
        {id:0,rolename:'基础'},
        {id:9,rolename:'管理'},
    ];

    //获取所有角色
    (function () {
        _API.rbac.roles().success(function (res) {
            $scope.all_roles = res;
        });
    })();

    //获取所有岗位
    function refresh() {
        _API.rank.all().success(function(res){
            $scope.ranks = res;
            $scope.rank = {};
            $scope.isEdit = false;
            for(var i in $scope.all_roles){
                $scope.all_roles[i].isActive = false;
            }
        });
    }
    refresh();

    //删除岗位
    $scope.del_rank = function (rank,e) {
        e.stopPropagation();
        swal({
                title: "<small>确认删除岗位 " + rank.name + " 吗?</small>",
                html:true,
                showCancelButton: true,
                closeOnConfirm: false,
                showLoaderOnConfirm: true,
                confirmButtonText:"确定",
                cancelButtonText:"取消",
                confirmButtonColor:"#3c6",
                animation:false,
            },
            function(){
                _API.rank.del(rank.id).success(function(){
                    swal.close();
                    refresh();
                    showToast('删除成功','ok');
                });
            });
    }

    //点击岗位
    $scope.ranks_role_show = function (rank,data) {
        $scope.isEdit = true;

        //修改颜色
        for(var i in data){
            data[i].isActive = false;
        };
        rank.isActive = true;

        $scope.roleid = [];
        _API.rank.get(rank.id).success(function(res){
            $scope.rank = res;
        });
        _API.rank.roles_for_rank(rank.id).success(function (res) {
            for(var i in $scope.all_roles){
                $scope.all_roles[i].isActive = false;
                for(var j in res){
                    if($scope.all_roles[i].id == res[j].role_id){
                        $scope.all_roles[i].isActive = true;
                    }
                }

            }
        });

    };

    // 点击增加岗位按钮
    $scope.addRank = function () {

        //切换右边标题文字
        $scope.isEdit = false;
        //不选中用户
        for(var i in $scope.ranks){
            $scope.ranks[i].isActive = false;
        };
        $('.input-name').focus();
        $scope.rank = {};
        $scope.roleid = [];
    };

    // 岗位信息提交||修改
    $scope.rank_submit = function () {
        var msg = {};
        if($scope.rank.id) msg.id = $scope.rank.id;

        if(!$scope.rank.name){
            showToast('名称不能为空','err');
            return;
        }else{
            msg.name = $scope.rank.name;
        }
        if($scope.rank.type) msg.type = $scope.rank.type;
        if($scope.rank.remark) msg.remark = $scope.rank.remark;

        console.log(msg,$scope.isEdit);
        if($scope.isEdit){
            //修改
            //岗位信息
            console.log($scope.isEdit);
            _API.rank.upd(msg).success(function(){
                refresh();
                showToast('修改岗位成功','ok');
            });

        }else{
            // 新增
            _API.rank.add(msg).success(function(){
                refresh();
                showToast('添加岗位成功','ok');
            });
        }

    };

    //添加｜｜删除角色
    $scope.check_role = function(role){
        if(!$scope.rank.id){
            showToast('请选择岗位','info');
            return;
        }
        if(role.isActive){
            //删除
            _API.rank.remove_role_from_rank($scope.rank.id,role.id).success(function () {
                showToast('删除角色成功','ok');
            });
            // console.log('del');
        }else{
            //增加
            _API.rank.add_role_to_rank($scope.rank.id,role.id).success(function () {
                showToast('添加角色成功','ok');
            });
            // console.log('add');
        }
        role.isActive = !role.isActive;
        // console.log(role.id);
    }
}];